import time
import csv
import os
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

from splider.common import CSVUtil
from splider.common import Progress_Tracker

urlroot = "https://car.autohome.com.cn"


# 车型列表
def get_config():
    current_dir = os.path.abspath('.')
    print(current_dir)

    model_csv_path = "data/model.csv"
    config_csv_path = "data/config.csv"
    point_key = "model_config"

    model_csv_file = os.path.join(current_dir, model_csv_path)
    config_csv_file = os.path.join(current_dir, config_csv_path)
    progress_file = os.path.join(current_dir, "data/progress.txt")

    # 读取当前进度
    Progress_Tracker.load_progress(progress_file)

    start_line = Progress_Tracker.get_progress_data()[point_key]

    webdriver_file = os.path.join(current_dir, 'resources/chromedriver')
    service = Service(executable_path=webdriver_file)

    # 使用 Service 对象初始化 Chrome
    driver = webdriver.Chrome(service=service)

    pre_series_name = ""
    # 读取并获得所有URL地   址，同时记录品牌名称
    with open(model_csv_file, 'r') as mcf, open(config_csv_file, 'a', newline='') as csf:  # 使用追加模式

        reader = csv.reader(mcf)
        writer = csv.writer(csf)
        for i, row in enumerate(reader):
            if i < start_line:
                continue

            # 01:读取整行
            brand = row[0]  # 读取第1列品牌
            series_name = row[1]  # 读取第2列车系
            model_name = row[2]  # 读取第3列车型
            config_url = row[6]  # 读取第4列车型配置url

            # 每次都会一次性读取所有车系下的车型配置，所以相同车系不需要读取两次配置
            if pre_series_name == series_name:
                Progress_Tracker.save_progress(progress_file, point_key, i + 1)
                continue

            pre_series_name = series_name

            # 访问页面
            driver.get(config_url)
            # 等待页面加载（根据需要调整等待时间）
            time.sleep(1)

            print(f"品牌={brand},车系名称={series_name},车型名称={model_name},车型配置url={config_url}")

            trs = driver.find_elements(By.CSS_SELECTOR, '.conbox .video-tips ~ .tbcs tr')

            # 获取class='carbox'下的div,div下的a的文本
            model_names = []
            for el in driver.find_elements(By.CSS_SELECTOR, '.carbox div a'):
                model_names.append(el.text)

            if len(trs) > 0:

                tds = [td for td in trs[0].find_elements(By.TAG_NAME, 'td') if td.text.strip() != '']

                th_datas = []
                td_datas = [[] for _ in range(len(tds))]

                for tr in trs:

                    th_elements = tr.find_elements(By.TAG_NAME, 'th')
                    td_elements = tr.find_elements(By.TAG_NAME, 'td')

                    # 跳过没有td的tr
                    if not td_elements:
                        continue

                    if th_elements:
                        th_datas.append(th_elements[0].text)

                    # Add text from each 'td' element to the corresponding list in 'td_data'
                    for j, td in enumerate(td_elements):
                        # 只获取不为空的列文本
                        if len(td_datas) > j:
                            td_datas[j].append(td.text)

                # config.csv的第一行才添加表头
                if (i == 0):
                    CSVUtil.save2csv(writer, "品牌", "车系名称", "车型名称", "车型配置url", *th_datas)

                for index , td_data  in enumerate(td_datas):
                    if td_data:  # 确保 td_data 不是空的
                        CSVUtil.save2csv(writer, brand, series_name, model_names[index], config_url, *td_data)

                csf.flush()
                print(f"{brand},{series_name},{model_name},{config_url}")
                print()

                # 在循环的最后，保存当前进度
                Progress_Tracker.save_progress(progress_file, point_key, i + 1)
                time.sleep(1)

    # 关闭浏览器
    driver.quit()


if __name__ == '__main__':
    get_config()
    print("处理结束")
